/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crear;

import apunte.Carrera;
import apunte.ManejoApunte;
import apunte.Materia;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author martinLequerica
 */
public class crearMateria extends JFrame {
    
    
    private JButton aceptar,cancelar,agregarOtro;
    private LinkedList<JTextField> lljt;
    private String [] etiquetas={"Nombre Mateira","jefeDeCatedra"};
    
    public crearMateria(){
        super("Crear Profesor");
        aceptar = new JButton("Aceptar");
        cancelar = new JButton("Cancelar");
        agregarOtro = new JButton("Agregar otro");
        lljt = new LinkedList();
        
        
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(300,150);
        setLocation(d.width/2,d.height/2);
        
        setLayout(new GridLayout(4, 2));
        
        for (int i =0 ; i<etiquetas.length;i++){
            
            
            lljt.add(new JTextField());
            add(new JLabel(etiquetas[i]));
            add(lljt.get(i));
            
            
            
        }
        add(agregarOtro);
        JButton fl = new JButton("");
        fl.setEnabled(false);
        add(fl);
        add(aceptar);
        add(cancelar);
        
        cancelar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
           
                setVisible(false);
                
                
            }
        });
        
        
        aceptar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
           
                String[] datos = traerDatos();
                try {
                    new ManejoApunte().guardarMateria(new Materia(datos));
                    setVisible(false);
                } catch (SQLException ex) {
                    Logger.getLogger(crearMateria.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }

            
        });
        
        agregarOtro.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
           
                String[] datos = traerDatos();
                try {
                    new ManejoApunte().guardarMateria(new Materia(datos));
                    borrarDatos();
                } catch (SQLException ex) {
                    Logger.getLogger(crearMateria.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                
                
                
            }

           
            
             
        });
        
        setVisible(true);
        
        
    }
    
    
    

    
    
    
      private String[] traerDatos() {
                
                String [] dato = new String[lljt.size()];
                for (int i =0 ; i<lljt.size();i++){
                    
                    dato[i] = lljt.get(i).getText();
                    
                }
                return dato;
                
            }
      
       private void borrarDatos() {
                for (int i= 0 ; i<lljt.size();i++){
                    
                    lljt.get(i).setText("");
                    
                }
            }
}
